/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.server.ticket.registry;

import java.util.Collection;

import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.qloudgen.sec.cache.Cache;
import org.qloudgen.sec.cache.CacheManager;

/**
 * <p>Title: Cached Tickets Registry</p>
 * <p>Description: Wrapper cached tickets registry implementation.</p>
 * <p>Copyright: qloudgen/sec 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public class CachedTicketRegistry extends AbstractDistributedTicketRegistry {
	/**
	 * Log output.
	 */
	private static final Logger logger = LoggerFactory.getLogger( CachedTicketRegistry.class );
	/**
	 * CAS default ticket cache name.
	 */
	private final static String DEFAULT_TICKET_CACHE = "CAS-TICKETS";
	/**
	 * Cache name.
	 */
	protected String cacheName = null;
	/**
	 * Cache manager.
	 */
	protected CacheManager cacheManager = null;
	
	/**
	 * Custom constructor.
	 *
	 * @param cacheManager
	 */
	public CachedTicketRegistry(CacheManager cacheManager) {
	    this( cacheManager , DEFAULT_TICKET_CACHE );
	}
	
	/**
	 * Custom constructor.
	 *
	 * @param cacheManager
	 */
	public CachedTicketRegistry(CacheManager cacheManager , String cacheName) {
	    this.cacheManager = cacheManager;
	    this.cacheName = cacheName;
	}
	
	/**
	 * Returns current cache name.
	 *
	 * @return String
	 */
	public String getCacheName() {
		return cacheName;
	}
	
	/**
	 * Sets current cache name.
	 *
	 * @param cacheName
	 */
	public void setCacheName(String cacheName) {
		this.cacheName = cacheName;
	}

	/**
	 * @see org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry
	 *
	 * @param ticket
	 */
	@Override
	protected void updateTicket(Ticket ticket) {
		logger.debug( "Starting update ticket...{}" , ticket.getId() );
		addTicket( ticket );
	}
	
	/**
	 * @see org.jasig.cas.ticket.registry.AbstractTicketRegistry
	 *
	 * @param ticket
	 */
	@Override
	public void addTicket(Ticket ticket) {
		logger.debug( "Starting add ticket...{}" , ticket.getId() );
		cacheManager.getCache( cacheName ).put( ticket.getId() , ticket );
	}
	
	/**
	 * @see org.jasig.cas.ticket.registry.AbstractTicketRegistry
	 *
	 * @param ticketId
	 * @return Ticket
	 */
	@Override
	public Ticket getTicket(String ticketId) {
		logger.debug( "Starting get ticket...{}" , ticketId );
		return getProxiedTicketInstance( ( Ticket )cacheManager.getCache( cacheName ).get( ticketId ) );
	}
	
	/**
	 * @see org.jasig.cas.ticket.registry.AbstractTicketRegistry
	 *
	 * @param ticketId
	 * @return boolean
	 */
	@Override
	public boolean deleteTicket(String ticketId) {
		logger.debug( "Starting delete ticket...{}" , ticketId );
		return ( cacheManager.getCache( cacheName ).remove( ticketId ) != null );
	}
	
	/**
	 * @see org.jasig.cas.ticket.registry.AbstractTicketRegistry
	 *
	 * @return Collection<Ticket>
	 */
	@Override
	public Collection<Ticket> getTickets() {
		Cache<String, Ticket> _tickets = cacheManager.getCache( cacheName );
		logger.debug( "Starting get all tickets...{}" , _tickets.size() );
		return _tickets.values();
	}
	
	/**
	 *
	 *
	 * @return boolean
	 */
	@Override
	protected boolean needsCallback() {
	    return true;
	}
}